<?php
require_once(__PHY_LIB.'/class/Item.class.php');
require_once(__PHY_LIB.'/func/Server.func.php');

class uAd extends Item {
/// definition and constructor ///
	function uAd($id=0, $field="") {
		Query::Query();
		$this->cfgKeyField("id");
		$this->cfgAllField("id,location,rank,obj_id,title,title_en,description,description_en,img1_url,click1_url");
		$this->cfgTable("unit_ad");
		if ("" == $field || "*" == $field) {
			$this->cfgField($this->mAllField);
		}
		else {
			$this->cfgField($field);
		}
		if (0 < $id) {
			$this->cfgWhere($this->mKeyField . "=" . $id);
			$this->load();
		}
		else {
			$this->set($this->mKeyField, $id);
		}
	}

	function invalid() {
		if (!$this->get('location')) {
			return 'location';
		}

		return false;
	}

/// 文件上传相关 ///
    function calcFileUrl($nID, $sFile) {
		if (!(0 < $nID)) { $nID = $this->getID(); }
        if (!(0 < $nID)) { return false; }
        if (!$sFile) { return false; }
        $sNumber = str_pad($nID, 9, '0', STR_PAD_LEFT);
		$sDir = "/".substr($sNumber, 0, 3)."/".substr($sNumber, 3, 3)."/".substr($sNumber, 6, 3)."/";
		return "/ad{$sDir}{$sFile}";
    }
    function saveFile($sSrcPath, $sField) {
        /// 得到图片信息 ///
        list($nOldW, $nOldH, $nType) = getimagesize($sSrcPath);
        if (IMAGETYPE_GIF == $nType) { $sExt = 'gif'; }
        elseif (IMAGETYPE_JPEG == $nType) { $sExt = 'jpg'; }
        elseif (IMAGETYPE_PNG == $nType) { $sExt = 'png'; }
		else { return false; } // 只支持gif/jpg/png
		/// 保存源文件 ///
		if (!($sUrl = $this->calcFileUrl(null, "{$sField}.{$sExt}"))) { return false; }
		mkdir_plus(dirname(__PHY_STO.$sUrl)); // 生成目录
		move_uploaded_file($sSrcPath, __PHY_STO.$sUrl); // 保存
		/// 修改 ///
		$this->set("{$sField}_url", $sUrl);
		return true;
	}

/// 排序相关 ///
	function calcNewRank() {
		$sQ = "SELECT MAX(rank) max_rank FROM unit_ad WHERE location='".$this->get('location')."'";
		$this->loadQuery($sQ);
		$r = $this->nextRecord(false);
		return (int)$r['max_rank'] + 1;
	}
	function rankTo($nRank) {
		if (!(0 < $this->getID())) { return false; }
		$nRank = max(1, (int)$nRank);
		if ($nRank > $this->get('rank')) { // 增大
			$oList = new uAdList();
			$oList->cfgWhere("location='".$this->get('location')."' AND rank>'".$this->get('rank')."' AND rank<='{$nRank}'");
			$oList->load();
			while ($o = $oList->nextItem()) {
				$o->set('rank', $o->get('rank') - 1); // 中间的统统-1
				$o->update();
			}
		}
		elseif ($nRank < $this->get('rank')) { // 减小
			$oList = new uAdList();
			$oList->cfgWhere("location='".$this->get('location')."' AND rank<'".$this->get('rank')."' AND rank>='{$nRank}'");
			$oList->load();
			while ($o = $oList->nextItem()) {
				$o->set('rank', $o->get('rank') + 1); // 中间的统统+1
				$o->update();
			}
		}
		else { // 相等，没变动
			return true;
		}
		/// 修改自己 ///
		$this->set('rank', $nRank);
		$this->update();
		return true;
	}

} /// end class uAd.


class uAdList extends ItemList {
	/// definition and constructor ///
	function uAdList($tablename=null) {
		ItemList::ItemList();
		$this->cfgClass("uAd");
		$this->cfgKeyField("id");
		$this->cfgAllField("id,location,rank,obj_id,title,title_en,description,description_en,img1_url,click1_url");
		$this->cfgTable(null == $tablename ? "unit_ad" : $tablename);
	}

} /// end class uAdList.


/// related functions ///
function uAd_i18n($nID, $sField) {
	static $rCache = array();
	if (!isset($rCache['LangFields'])) {
		$rCache['LangFields'] = '|title|description';
	}
	if (!isset($rCache[$nID])) {
		$oOne = new uAd($nID);
		$rCache[$nID] = $oOne->getData();
	}
	if ('en' == $_SESSION['LANG'] && strpos($rCache['LangFields'], $sField)) {
		$sField .= '_en';
		return $rCache[$nID][$sField];
	}
	else { return $rCache[$nID][$sField]; }
}
?>
